package kr.hkit.powerjava.chapter16.lab;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Counter extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JPanel panelText;
	private JPanel panelButton;
	private JLabel lbl01;
	private JLabel lbl02;
	private JButton btnIncrease;
	private JButton btnDecrease;
	private int count = 0;

	public String getLbl02(String text) {
		return lbl02.getText();
	}

	public void setLbl02(String text) {
		this.lbl02.setText(text);
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Counter frame = new Counter();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Counter() {
		initialize();
	}
	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
		
		panelText = new JPanel();
		contentPane.add(panelText);
		
		lbl01 = new JLabel("Counter");
		lbl01.setHorizontalAlignment(SwingConstants.CENTER);
		panelText.add(lbl01);
		
		lbl02 = new JLabel(count + "");
		lbl02.setFont(new Font("Serif", Font.BOLD|Font.ITALIC, 100));
		panelText.add(lbl02);
		
		panelButton = new JPanel();
		contentPane.add(panelButton);
		
		btnIncrease = new JButton("카운터 증가");
		btnIncrease.addActionListener(this);
		panelButton.add(btnIncrease);
		
		btnDecrease = new JButton("카운터 감소");
		btnDecrease.addActionListener(this);
		panelButton.add(btnDecrease);
		
		pack();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnDecrease) {
			actionPerformedBtnDecrease(e);
		}
		if (e.getSource() == btnIncrease) {
			actionPerformedBtnIncrese(e);
		}
	}
	protected void actionPerformedBtnIncrese(ActionEvent e) {
		count++;
		lbl02.setText(count + "");
	}
	protected void actionPerformedBtnDecrease(ActionEvent e) {
		count--;
		lbl02.setText(count + "");
	}
}
